package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.utils.IdSequeue;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VideosApiImpl implements VideosApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdSequeue idSequeue;

    @Override
    public String save(Video video) {
        video.setId(ObjectId.get());
        video.setVid(idSequeue.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        mongoTemplate.save(video);
        return video.getId().toHexString();
    }

    @Override
    public PageResult<Video> findVideoList(int page, int pagesize, long userId) {
        Query query = new Query()
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pagesize));
        List<Video> list = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count,pagesize,pages,page,list);
    }

    @Override
    public void userFocus(Long userId, Long followUserId) {
        FollowUser followUser = new FollowUser();
        followUser.setId(ObjectId.get());
        followUser.setFollowUserId(followUserId);
        followUser.setUserId(userId);
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);
    }

    @Override
    public void userUnFocus(Long userId, Long followUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("followUserId").is(followUserId));
        mongoTemplate.remove(query, FollowUser.class);
    }

    @Override
    public PageResult<Video> findByIdVideoList(int page, int pagesize, Long uid) {
        Query query = new Query(Criteria.where("userId").is(uid))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pagesize));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count,pagesize,pages,page,videoList);
    }

    @Override
    public Video findByid(String videoId) {
        Query query = new Query(Criteria.where("_id").is(new ObjectId(videoId)));
        return mongoTemplate.findOne(query,Video.class);
    }

    @Override
    public List<Video> findByVids(List<Long> vidList) {
        return mongoTemplate.find(new Query(Criteria.where("vid").in(vidList)),Video.class);
    }
}
